//
//  GuideViewController.swift
//  ManBetX
//
//  Created by Cocoa on 2020/3/30.
//  Copyright © 2020 Fullen. All rights reserved.
//

import UIKit

// 引导页
class GuideViewController: UIViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {

    
    var allViewControllers: [UIViewController] = NSMutableArray.init() as! [UIViewController]
    @IBOutlet weak var pageControl: CocoaPageControl!
    var currentPage = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configurePageViewController()
        self.view.bringSubviewToFront(self.pageControl)
        self.pageControl.itemCurrentCornerRadius = 2.5
        self.pageControl.itemNormalCornerRadius = 2.5
    }
    
    func configurePageViewController() {
        
        // 配置引导页子页面
        for index in 1...3 {
            
            let guideVC =  GuideSubViewController.instanceFromStoryboard()!
            if index == 1 {
                guideVC.subType = .first
            }else if index == 2 {
                guideVC.subType = .second
            }else if index == 3 {
                guideVC.subType = .third
            }else {
                continue
            }
            self.allViewControllers.append(guideVC)
        }
        
        //transitionStyle:转换样式，有PageCurl和Scroll两种
        //navigationOrientation:导航方向，有Horizontal和Vertical两种
        //options: UIPageViewControllerOptionSpineLocationKey---书脊的位置
        //UIPageViewControllerOptionInterPageSpacingKey---每页的间距
        let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        
        pageViewController.dataSource = self
        pageViewController.delegate = self
        
        self.addChild(pageViewController)
        self.view.addSubview(pageViewController.view)
        
        //设置初始引导页
        pageViewController.setViewControllers([allViewControllers.first!], direction: .forward, animated: true, completion: nil)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        
        let tempController: GuideSubViewController = pendingViewControllers.first as! GuideSubViewController
        if tempController.subType == .third {
            self.pageControl.isHidden = true
        }else {
            self.pageControl.isHidden = false
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if finished && completed {
            
            if let currentVC = pageViewController.viewControllers?.last {

                self.currentPage = allViewControllers.firstIndex(of: currentVC)!
                self.pageControl.currentPage = self.currentPage
            }
        }
        
        let tempController: GuideSubViewController = pageViewController.viewControllers?.last as! GuideSubViewController
        if tempController.subType == .third {
            self.pageControl.isHidden = true
        }else {
            self.pageControl.isHidden = false
        }
    }
        
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        self.currentPage -= 1
        if self.currentPage < 0 {
            self.currentPage = 0
            return nil
        }
        
        let tempController: GuideSubViewController = viewController as! GuideSubViewController
        if tempController.subType == .third {
            self.pageControl.isHidden = true
        }else {
            self.pageControl.isHidden = false
        }
        return allViewControllers[self.currentPage] as UIViewController

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        self.currentPage += 1
        if self.currentPage >= allViewControllers.count {
            self.currentPage = allViewControllers.count - 1
            return nil
        }
        let tempController: GuideSubViewController = viewController as! GuideSubViewController
        if tempController.subType == .third {
            self.pageControl.isHidden = true
        }else {
            self.pageControl.isHidden = false
        }
        
        return allViewControllers[self.currentPage] as UIViewController
    }
}
